package org.lex.dbq.view.profile;

import java.util.Collection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.lex.dbq.model.bo.CommonObject;

public class TableUtils {
	public static <T extends CommonObject> Table createTable(Composite parent, Collection<T> datas) {
		Table table = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
		table.setHeaderVisible(true);
		// column
		TableColumn id = new TableColumn(table, SWT.LEFT);
		id.setText("ID");

		TableColumn name = new TableColumn(table, SWT.LEFT);
		name.setText("Name");

		TableColumn desc = new TableColumn(table, SWT.LEFT);
		desc.setText("Description");

		// data
		for (T t : datas) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(0, t.getId());
			CommonObject o = (CommonObject) t;
			item.setText(1, o.getName());
			item.setText(2, o.getDescription());
		}
		// pack
		TableColumn[] columns = new TableColumn[] { id, name, desc };
		for (TableColumn c : columns) {
			c.pack();
		}
		return table;
	}
}
